package com.liyiqing.secondhandmall.orderservice.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.liyiqing.commonservice.vo.ProductShowVo;
import com.liyiqing.secondhandmall.orderservice.feign.MemberFeignService;
import com.liyiqing.secondhandmall.orderservice.feign.ProductFeignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.liyiqing.secondhandmall.orderservice.entity.OrderInfoEntity;
import com.liyiqing.secondhandmall.orderservice.service.OrderInfoService;
import com.liyiqing.commonservice.utils.PageUtils;
import com.liyiqing.commonservice.utils.R;

import javax.annotation.Resource;


/**
 * 订单信息
 *
 * @author LiYiQing
 * @email 1570865668@qq.com
 * @date 2022-02-18 23:27:24
 */
@RestController
@RequestMapping("orderService/orderInfo")
public class OrderInfoController {
    @Autowired
    private OrderInfoService orderInfoService;

    //注入远程调用接口
    @Resource
    MemberFeignService memberFeignService;

    @Resource
    ProductFeignService productFeignService;




    /**
     * 测试远程服务 product-service：
     * 处理data处的数据
     */
    @RequestMapping("/test/{id}")
    public R test(@PathVariable("id") Long id){
        //获取某账号发布的所有商品信息
        R r = productFeignService.selectProductByMemberId(id);
        try{

            //1.data处为单个对象时
//            Object o = r.get("data");
//            JSONObject json = (JSONObject) JSON.toJSON(o);
//            ProductShowVo productShowVo = JSONObject.parseObject(String.valueOf(json), ProductShowVo.class);
//            System.out.println("productShowVo对象的内容为："+productShowVo);


            //2.data处为list集合时
            Object o = r.get("data");//是一个List<ProductShowVo>集合
            JSONArray json = (JSONArray) JSON.toJSON(o);
            List<ProductShowVo> dataList = JSONArray.parseArray(String.valueOf(json), ProductShowVo.class);
            System.out.println("dataList集合中的内容为："+dataList);
            for (ProductShowVo productShowVo:dataList ) {
                System.out.println("productShowVo对象的内容为："+productShowVo);
            }

        }catch (Exception e){
            e.printStackTrace();
        }

        return R.ok();
    }




    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = orderInfoService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		OrderInfoEntity orderInfo = orderInfoService.getById(id);

        return R.ok().put("orderInfo", orderInfo);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody OrderInfoEntity orderInfo){
		orderInfoService.save(orderInfo);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody OrderInfoEntity orderInfo){
		orderInfoService.updateById(orderInfo);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		orderInfoService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
